package com.hnguigu.health.houduan.xsg.controller_xsg;

import com.github.pagehelper.PageInfo;
import com.hnguigu.health.houduan.xsg.service_xsg.impl.TuserServiceImpl;
import com.hnguigu.health.pojo.*;

import com.hnguigu.health.utils.jiangKangPojo;
import com.sun.net.httpserver.HttpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class xsg {
    @Autowired(required = false)
    TuserServiceImpl TuserMapper;

    @RequestMapping("login")
    @ResponseBody
    public TUser login(TUser record, String name, String pwd,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        record.setUsername(name);
        record.setPassword(pwd);
        session.setAttribute("list",name);

        TUser login = TuserMapper.login(record);
        if(login!=null){
            Date date = new Date();
            SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

            String time=sim.format(date);
            Logtoday logtoday = new Logtoday();

            logtoday.setLogName(name);
            logtoday.setCredate(time);
            TuserMapper.insert(logtoday);
        }
        return  login;
    }


    @RequestMapping("TmenuAllList")
    @ResponseBody
    public   Map<String, List<TMenu>> TmenuAllList(HttpServletRequest req, HttpServletResponse resp,String name) throws ServletException, IOException, SQLException {
        System.out.println(name);
        String aoccutn =(String) req.getSession().getAttribute("list");
        System.out.println(aoccutn);
        Map<String, List<TMenu>> menuAll = TuserMapper.findMenuAll(name);
        return  menuAll;
    }

    @RequestMapping("/TmemberAllList")
    @ResponseBody
    public PageVo<TMember> findAllList(Integer page, Integer num, TMember emp,String name){
        System.out.println(name);



            emp.setName(name);



        PageVo<TMember> tMemberPageVo = TuserMapper.TmemberAllList(page, num, emp);
        System.out.println(tMemberPageVo);
        return tMemberPageVo;
    }

    @RequestMapping("/TmemberAllDel")
    @ResponseBody
    public boolean del(String id) throws ServletException, IOException {
        System.out.println(id);
        String[] split = id.split(",");
        boolean del1 = TuserMapper.TmemberAllDel(split);
        System.out.println(del1);
        return true;
    }
    @GetMapping("/TMemberAdd")
    @ResponseBody
    public boolean insertSelective(TMember record,HttpServletRequest req,String regtime,String birthday) {

        HttpSession session = req.getSession();
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyyMMdd");

        String time=sim.format(date);
        Bh bh = TuserMapper.selectByPrimaryKey(1);
        String ckId = bh.gettFilenumber();
        int i = Integer.parseInt(ckId);
        String s = String.format("%02d", i+1);
        bh.settFilenumber(s);


        TuserMapper.updateByPrimaryKeySelective(bh);
        


        String kk =time+bh.gettFilenumber();









        record.setFilenumber(kk);

        System.out.println(record);
        int i1 = TuserMapper.insertSelective(record);
        if(i1>0) {
            session.setAttribute("kk",kk);
            return true;
        }
        return false;
    }

    @GetMapping("/TMemberDynamicinfoAdd")
    @ResponseBody
    public void insertSelective(TMemberDynamicinfo record,HttpServletRequest req) {
        String aoccutn =(String) req.getSession().getAttribute("kk");
        System.out.println(aoccutn);
        record.settMemberId(aoccutn);
        TuserMapper.insertSelective(record);
    }

    @GetMapping("/TMemberInfoinsertSelective")
    @ResponseBody
    public void TMemberInfoinsertSelective(TMemberInfo record ,HttpServletRequest req) {
        String aoccutn =(String) req.getSession().getAttribute("kk");
        int i = Integer.parseInt(aoccutn);
        record.settMemberId(i);
        TuserMapper.insertSelective(record);
    }


    @RequestMapping("/TMemberExaminationByidAll")
    @ResponseBody
    public TMemberDynamicinfo TMemberExaminationByidAll(String id) throws ServletException, IOException {
        TMemberDynamicinfo tMemberDynamicinfo = TuserMapper.TMemberExaminationByidAll(id);
        return  tMemberDynamicinfo;
    }

    @GetMapping("/TMemberDynamicinfoUpdate")
    @ResponseBody
    public void updateByPrimaryKey(TMemberDynamicinfo record) {
        System.out.println(record);
        TuserMapper.updateByPrimaryKey(record);
    }


    @GetMapping("/TMemberUpdate")
    @ResponseBody
    public void updateByPrimaryKeySelective(TMember record) {
        System.out.println(record);
        TuserMapper.updateByPrimaryKeySelective(record);
    }


    @RequestMapping("/TMemberExaminationAllDel")
    @ResponseBody
    public boolean TMemberExaminationAllDel(String id) throws ServletException, IOException {
        System.out.println(id);
        String[] split = id.split(",");
        boolean del1 = TuserMapper.TMemberExaminationAllDel(split);
        System.out.println(del1);
        return true;
    }


    @RequestMapping("/t_healthmgrAllList")
    @ResponseBody
    public List<THealthmgr> t_healthmgrAllList()  {
        List<THealthmgr> tHealthmgrs = TuserMapper.t_healthmgrAllList();
        return tHealthmgrs;
    }


    @RequestMapping("/TmembersibiaoAllList")
    @ResponseBody
    public PageVo<TMember> TmembersibiaoAllList(Integer page, Integer num){
        PageVo<TMember> tMemberPageVo = TuserMapper.TmembersibiaoAllList(page, num);
        return tMemberPageVo;
    }

    @RequestMapping("/ImgALlLIst")
    @ResponseBody
    public List<Img> ImgALlLIst(String id){
        List<Img> imgs = TuserMapper.ImgALlLIst(id);
        return imgs;
    }


    @RequestMapping("/selectAllYuefen")
    @ResponseBody
    public List<TMember> selectAllYuefen(String fen){
        System.out.println(fen);
        List<TMember> tMembers = TuserMapper.selectAllYuefen(fen);

        return tMembers;
    }

    @RequestMapping("/selectToderfenlei")
    @ResponseBody
    public List<TOrder> selectToderfenlei(String id){
        List<TOrder> tOrders = TuserMapper.selectToderfenlei(id);
        return tOrders;
    }

    @RequestMapping("/shangchuanByAll")
    @ResponseBody
    public List<TMemberExamination> shangchuanByAll(String id){
        List<TMemberExamination> TMemberExamination = TuserMapper.shangchuanByAll(id);
        return TMemberExamination;
    }

    @RequestMapping("/logtodayByAll")
    @ResponseBody
    public List<Logtoday> logtodayByAll(){
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

        String time=sim.format(date);
        List<Logtoday> logtodays = TuserMapper.logtodayByAll(time);
        return  logtodays;
    }

    @RequestMapping("/yuyuecgAll")
    @ResponseBody
    public List<TOrder> yuyuecgAll(){
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

        String time=sim.format(date);
        List<TOrder> tOrders = TuserMapper.yuyuecgAll(time);

        return tOrders;
    }


    @RequestMapping("/selectTorder")
    @ResponseBody
    public List<TOrder> selectTorder(String id) throws ServletException, IOException {
        System.out.println(id);
        String[] split = id.split(",");
        System.out.println(split);
        List<TOrder> tOrders = TuserMapper.selectTorder(split);
        System.out.println(tOrders);
        return tOrders;
    }

    @RequestMapping("/TboDyTypeAll")
    @ResponseBody
    public List<TBodyType> TboDyTypeAll(){
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

        String time=sim.format(date);
        List<TBodyType> tBodyTypes = TuserMapper.TboDyTypeAll(time);

        return tBodyTypes;
    }
        //ssssssssssssssssssssssssssssssssssssssssssssssssssssssss
    @RequestMapping("/yuyuecgAll1")
    @ResponseBody
    public  List<TOrder> yuyuecgAll1(){
        List<TOrder> tOrders = TuserMapper.yuyuecgAll1();
        return tOrders;
    }

    @RequestMapping("/TboDyTypeAll1")
    @ResponseBody
    public  List<TBodyType> TboDyTypeAll1(){
        List<TBodyType> tBodyTypes = TuserMapper.TboDyTypeAll1();
        return tBodyTypes;
    }

    @RequestMapping("/t_member_exam1")
    @ResponseBody
    public List<TMemberExamination> t_member_exam1(String id){
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

        String time=sim.format(date);
        List<TMemberExamination> tMemberExaminations = TuserMapper.t_member_exam1(time);
        return tMemberExaminations;
    }
    @RequestMapping("/t_member_exam0")
    @ResponseBody
    public List<TMemberExamination> t_member_exam0(String id){
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

        String time=sim.format(date);
        List<TMemberExamination> tMemberExaminations = TuserMapper.t_member_exam0(time);
        return tMemberExaminations;

    }
    @RequestMapping("/t_member_exam3")
    @ResponseBody
    public List<TMemberExamination> t_member_exam3(){
        List<TMemberExamination> tMemberExaminations = TuserMapper.t_member_exam3();
        return tMemberExaminations;
    }
    @RequestMapping("/t_member_exam4")
    @ResponseBody
    public List<TMemberExamination> t_member_exam4(){
        List<TMemberExamination> tMemberExaminations = TuserMapper.t_member_exam4();
        return tMemberExaminations;

    }

    @RequestMapping("/TQusetionEvalluasiAllList")
    @ResponseBody
    public List<TQuestionEvaluation> TQusetionEvalluasiAllList(){
        return TuserMapper.TQusetionEvalluasiAllList();

    }

    @RequestMapping("/count")
    @ResponseBody
    public List<TInterventionprogrammestable> count(){
        return TuserMapper.count();

    }

    @RequestMapping("/Tmecount")
    @ResponseBody
    public List<TMember> Tmecount(){
        return TuserMapper.Tmecount();

    }

    @RequestMapping("/TmecountData")
    @ResponseBody
    public  List<TMember> TmecountData(String id){
        Date date = new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");

        String time=sim.format(date);
        return TuserMapper.TmecountData(time);

    }

    @RequestMapping("/t_followCount")
    @ResponseBody
    public List<TFollowupjournal> t_followCount(){
        return TuserMapper.t_followCount();

    }



    @RequestMapping("/TuserAllList")
    @ResponseBody
    public PageVo<TUser> TuserAllList(Integer page, Integer num){
        PageVo<TUser> tUserPageVo = TuserMapper.TuserAllList(page, num);
        return  tUserPageVo;
    }

    @GetMapping("/TUserinsertSelective")
    @ResponseBody
    public int insertSelective(TUser record) {
        int i = TuserMapper.insertSelective(record);
        Integer id = record.getId();
        String username = record.getUsername();

        if (i>0){
            TUserRole tUserRole = new TUserRole();
            tUserRole.setUserId(id);
            tUserRole.setRoleId(2);
            int i1 = TuserMapper.insertSelective(tUserRole);
            if(i1>0){
                THealthmgr tHealthmgr1 = TuserMapper.Lastbean1();
                String lastbean = tHealthmgr1.getHealthMgrId().toString();
                String newTmid="";
                String substring="";
                if(lastbean==null){
                    newTmid="1001";
                }else{
                    substring = lastbean.substring(2, lastbean.length());
                }

                if(Integer.parseInt(substring)<10){
                    newTmid= "100"+(Integer.parseInt(substring)+1);
                }
                if(Integer.parseInt(substring)>=10){
                    newTmid= "10"+(Integer.parseInt(substring)+1);
                }
                if(Integer.parseInt(substring)>=100){
                    newTmid= "1"+(Integer.parseInt(substring)+1);
                }
                THealthmgr tHealthmgr = new THealthmgr();
                int i2 = Integer.parseInt(newTmid);
                tHealthmgr.setHealthMgrId(i2);
                tHealthmgr.setHealthMgrName(username);
                tHealthmgr.setStatus(0);
                tHealthmgr.setRemark1("高级健康管理师");

                TuserMapper.insertSelective(tHealthmgr);
            }
        }
        return 1;
    }

    @RequestMapping("/TRoleAllList")
    @ResponseBody
    public TRole TRoleAllList(String name){
        TRole tRole = TuserMapper.TRoleAllList(name);
        return  tRole;
    }

}
